﻿using LeafTransmission.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace LeafTransmission.Entity.ConfigEntity
{
    /// <summary>
    /// 网络配置实体类
    /// </summary>
    internal class NetworkConfigEntity
    {
        private int _udpPort;
        private int _tcpServerPort;
        private int _tcpRemotePort;
        public int UDPPort
        {
            get { return _udpPort; }
            set
            {
                if (value.GetType() != typeof(int))
                {
                    _udpPort = int.Parse(value.ToString().Trim());
                }
                else
                {
                    _udpPort = value;
                }

            }
        }

        public int TCPServerPort
        {
            get { return _tcpServerPort; }
            set
            {
                if (value.GetType() != typeof(int))
                {
                    _tcpServerPort = int.Parse(value.ToString().Trim());
                }
                else
                {
                    _tcpServerPort = value;
                }

            }
        }

        public int TCPRemotePort
        {
            get { return _tcpRemotePort; }
            set
            {
                if (value.GetType() != typeof(int))
                {
                    _tcpRemotePort = int.Parse(value.ToString().Trim());
                }
                else
                {
                    _tcpRemotePort = value;
                }

            }
        }

        public List<NICEntity> NICEntities { get; set; } = new List<NICEntity>();

        public List<NetworkSegmentEntity> NetworkSegmentEntities { get; set; } = new List<NetworkSegmentEntity>();

        public NetworkConfigEntity() { }

        public NetworkConfigEntity(int udpPort, int tcpServerPort, int tcpRemotePort, List<NICEntity> nicEntities, List<NetworkSegmentEntity> networkSegmentEntities)
        {
            UDPPort = udpPort;
            TCPServerPort = tcpServerPort;
            TCPRemotePort = tcpRemotePort;
            NICEntities = nicEntities;
            NetworkSegmentEntities = networkSegmentEntities;
        }

        /*public List<string> GetAllIPv4Address()
        {
            if (NICEntities == null) return null;

            List<string> result = new List<string>();
            foreach (NICEntity item in NICEntities)
            {
                if (item.OperationalStatus == "Up")
                {
                    result.Add(item.IPv4Address);
                }
            }
            return GetAllLegalIPv4Address(result);
        }*/
        public string GetSelectedIPv4Address()
        {
            string selectedIPAddr = null;
            if (NICEntities == null) return null;
            foreach (NICEntity item in NICEntities)
            {
                if (item.IPv4Address.Length > 0 && item.Selected)
                {
                    selectedIPAddr = item.IPv4Address;
                }
            }
            return selectedIPAddr;
        }
        public List<string> GetAllIPv4Address()
        {
            if (NICEntities == null) return null;

            List<string> result = new List<string>();
            foreach (NICEntity item in NICEntities)
            {
                result.Add(item.IPv4Address);
            }
            return result;
        }

        public static List<string> GetAllLegalIPv4Address(List<string> ipv4Addresses)
        {
            var ipv4AddressesList = new List<string>();
            foreach (string ipv4Address in ipv4Addresses)
            {
                if (ipv4Address != "" && 
                    ipv4Address.Length > 0 && 
                    NICUtil.REGEX.IsMatch(ipv4Address))
                {
                    ipv4AddressesList.Add(ipv4Address);
                }
            }

            return ipv4AddressesList;
        }

        public int GeSelectedSegmentIndex()
        {
            int selectedIndex = -1;
            if (NetworkSegmentEntities == null) { return selectedIndex; }
            
            for (int i = 0; i < NetworkSegmentEntities.Count; i++)
            {
                if (NetworkSegmentEntities[i].Selected == true)
                {
                    selectedIndex = i;
                }
            }
            return selectedIndex;
        }

        public int GetSelectedNICIndex()
        {
            int selectedIndex = -1;
            if (NICEntities == null) { return selectedIndex; }

            for (int i = 0; i < NICEntities.Count; i++)
            {
                if (NICEntities[i].Selected == true)
                {
                    selectedIndex = i;
                }
            }
            return selectedIndex;
        }

    }
}
